package cn.zwy.day1;

import static org.objectweb.asm.Opcodes.*;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.ModuleVisitor;
import org.objectweb.asm.TypePath;

public class ClassPrinter extends ClassVisitor {

    public ClassPrinter() {
        // ASM6支持JAVA9
        // ASM5支持JAVA8
        // ASM4支持JAVA7
        super(ASM6);
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        // TODO Auto-generated method stub
        super.visit(version, access, name, signature, superName, interfaces);

        // 编译级别
        for (int i = 0; i < 4; i++) {
            int jdkVersionInteger = 6 + i;
            if (version == V1_6 + i) {
                System.out.println("编译时对应的JDK版本:jdk" + jdkVersionInteger);
            }
        }

        System.out.print(((access & ACC_PUBLIC) == ACC_PUBLIC ? "public " : ""));
        // System.out.println(((access & ACC_SUPER) == ACC_SUPER ? "super" :
        // ""));
//        System.out.print(name);
//        System.out.print(" extends " + superName);
//        System.out.print(" implements " + interfaces[0]);
//        System.out.println(" {");
    }

    @Override
    public void visitSource(String source, String debug) {
        // TODO Auto-generated method stub
        // super.visitSource(source, debug);
        System.out.println("source:" + source + ", debug:" + debug);
    }

    @Override
    public ModuleVisitor visitModule(String name, int access, String version) {
        // TODO Auto-generated method stub
        return super.visitModule(name, access, version);
    }

    @Override
    public void visitOuterClass(String owner, String name, String desc) {
        // TODO Auto-generated method stub
        super.visitOuterClass(owner, name, desc);
    }

    @Override
    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
        // TODO Auto-generated method stub
        // System.out.println(desc + " " + visible);
        return super.visitAnnotation(desc, visible);
    }

    @Override
    public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
        // TODO Auto-generated method stub
        return super.visitTypeAnnotation(typeRef, typePath, desc, visible);
    }

    @Override
    public void visitAttribute(Attribute attr) {
        // TODO Auto-generated method stub
        super.visitAttribute(attr);
    }

    @Override
    public void visitInnerClass(String name, String outerName, String innerName, int access) {
        // TODO Auto-generated method stub
        super.visitInnerClass(name, outerName, innerName, access);
    }

    @Override
    public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
        // System.out.println(desc + " " + name + " = " + value + " signature:" + signature + " ; ");

        return super.visitField(access, name, desc, signature, value);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        // TODO Auto-generated method stub
        // System.out.println(name + " " + desc + " " + signature + " " + exceptions);
        MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions);
        return new MethodPrinter(methodVisitor);
    }

    @Override
    public void visitEnd() {
        // TODO Auto-generated method stub
        super.visitEnd();
        // System.out.println(" }");
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        return super.equals(obj);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }

    @Override
    protected void finalize() throws Throwable {
        // TODO Auto-generated method stub
        super.finalize();
    }

}
